package cn.tedu.erp.project.controller;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.pojo.dto.DeptAddNewDTO;
import cn.tedu.erp.project.pojo.dto.DeptUpdateDTO;
import cn.tedu.erp.project.pojo.dto.PermissionAddNewDTO;
import cn.tedu.erp.project.pojo.dto.PermissionUpdateDTO;
import cn.tedu.erp.project.pojo.vo.*;
import cn.tedu.erp.project.service.IDeptService;
import cn.tedu.erp.project.service.IPermissionService;
import cn.tedu.erp.project.web.JsonResult;
import cn.tedu.erp.project.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/permissions")
@Api(tags = "04.权限管理模块")
public class PermissionController {

    public PermissionController() {
        log.debug("创建控制器对象:PermissionController");
    }

    @Autowired
    IPermissionService service;

    //http://localhost:9090/permissions/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加权限")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/erp_pwdb/permission/add-new')")
    public JsonResult<Void> addNew(PermissionAddNewDTO permissionAddNewDTO) {
        log.debug("开始处理[添加权限]的请求,请求参数:{}", permissionAddNewDTO);
        service.addNew(permissionAddNewDTO);
        return JsonResult.ok();
    }

    //http://localhost:9090/permissions/0-9/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除权限")
    @PreAuthorize("hasAuthority('/erp_pwdb/permission/delete')")//检查用户是否有权限
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "long")
    )
    public JsonResult<Void> delete(@PathVariable @Range(min = 17, message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理[删除权限]的请求,请求参数:{}", id);
        service.deleteById(id);
        return JsonResult.ok();
    }

    //http://localhost:9090/permissions/0-9/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改权限")
    @PreAuthorize("hasAuthority('/erp_pwdb/permission/update')")//检查用户是否有权限
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "long")
    )
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 17, message = "请提交有效的ID值!") Long id,
                                           @Valid PermissionUpdateDTO permissionUpdateDTO) {
        log.debug("开始处理修改权限的请求,参数为id={},修改的数据={}", id, permissionUpdateDTO);
        service.updateById(id, permissionUpdateDTO);
        return JsonResult.ok();
    }

    //http://localhost:9090/permissions/0-9
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("查询权限详情")
    @PreAuthorize("hasAuthority('/erp_pwdb/permission/read')")//检查用户是否有权限
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "long")
    )
    public JsonResult<PermissionStandardVO> getStandardById(@PathVariable @Range(min = 17, message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理查询权限详情的业务,参数为id={}", id);
        if (id < IPermissionService.PRIVILEGE) {
            String message = "查询失败,被查询的权限不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //调用permissionMapper的getStandardVO方法
        PermissionStandardVO standardVO = service.getStandardById(id);
        if (standardVO == null) {
            String message = "查询失败,被查询的权限不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return JsonResult.ok(standardVO);
    }

    //http://localhost:9090/permissions
    @GetMapping("")
    @ApiOperation("查询权限列表")
    @PreAuthorize("hasAuthority('/erp_pwdb/permission/read')")//检查用户是否有权限
    @ApiOperationSupport(order = 410)
    public JsonResult<List<PermissionListVO>> list() {
        log.debug("开始处理[查询权限]的请求,无参数");
        List<PermissionListVO> list = service.list();
        return JsonResult.ok(list);
    }
}
